package classes{
	import beniAS3Lib.utils.BeniAry;
	
	import classes.vo.CommandData;
	import classes.vo.EventData;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class CreateEvent{
		
		
		private var _modelLocator:ModelLocator = ModelLocator.item;
		
		public function CreateEvent(){
			createEventType();
			createCtrl();
			// add package..
			createImportStatement();
		}
		
		
		
		
		
		
		
		
		
		private function createEventType():void{
			var content:String = '';
			var templateStr:String = "public static const #Name#:String = '#TypeString#';";
			var ary:Array = _modelLocator.eventColl.source;
			var item:EventData;
			var i:int;
			var str:String;
			var file:File;
			var fs:FileStream;
			var fileContent:String;
			for (i=0; i<ary.length; i++){
				item = ary[i] as EventData;
				str = templateStr.replace('#Name#', item.name);
				str = str.replace('#TypeString#', getTypeStr(item.name));
				content += str + '\n\t\t';
			}
			file = new File(_modelLocator.evtTypePath);
			fs = new FileStream();
			fs.open(file, FileMode.READ);
			fileContent = fs.readUTFBytes(fs.bytesAvailable);
			fileContent = fileContent.replace('#DefineConstData#', content);
			fs.close();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(fileContent);
			fs.close();
		}
		private function createCtrl():void{
			var content:String = '';
			var templateStr:String = "addCommand('EventType.#Type#, #Class#');";
			var ary:Array = _modelLocator.eventColl.source;
			var item:EventData;
			var i:int;
			var file:File;
			var fs:FileStream;
			var str:String;
			var fileContent:String;
			for(i=0; i<ary.length; i++){
				item = ary[i] as EventData;
				if (item.command != ''){
					str = templateStr.replace('#Type#', item.name);
					str = str.replace('#Class#', item.command);
					content += str + '\n\t\t\t\t';
				}
			}
			file = new File(_modelLocator.fontCtrlPath);
			fs = new FileStream();
			fs.open(file, FileMode.READ);
			fileContent = fs.readUTFBytes(fs.bytesAvailable);
			fileContent = fileContent.replace('#AddCommand#', content);
			fs.close();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(fileContent);
			fs.close();
		}

		
		private function getTypeStr(type:String):String{
			var result:String = '';
			var ary:Array = type.split('_');
			var i:int;
			var firstChr:String;
			ary[0] = String(ary[0]).toLowerCase();
			result = ary[0];
			for (i=1; i<ary.length; i++){
				ary[i] = String(ary[i]).toLowerCase();
				firstChr = String(ary[i]).charAt(0).toUpperCase();
				ary[i] = String(ary[i]).substring(1, String(ary[i]).length);
				ary[i] = firstChr + ary[i];
				result += ary[i];
			}
			return result;
		}
		private function createImportStatement():void{
			var content:String = '';
			var ary:Array = _modelLocator.commandColl.source;
			var packageAry:Array = [];
			var item:CommandData;
			var tempAry:Array;
			var packageName:String;
			var file:File;
			var fileContent:String;
			var fs:FileStream;
			for (var i:int=0; i<ary.length; i++){
				item = ary[i] as CommandData;
				tempAry = item.name.split('/');
				if (tempAry.length > 1){
					packageName = _modelLocator.packageName;
					for (var j:int=0; j<tempAry.length-1; j++){
						packageName += '.' + tempAry[j];
					}
					BeniAry.pushUnique(packageAry, packageName);
				}
			}
			
			for(i=0; i<packageAry.length; i++){
				content += 'import ' + packageAry[i] + '.*;\n\t';
			}
			
			file = new File(_modelLocator.fontCtrlPath);
			fs = new FileStream();
			fs.open(file, FileMode.READ);
			fileContent = fs.readUTFBytes(fs.bytesAvailable);
			fileContent = fileContent.replace('#ImportClass#', content);
			fs.close();
			fs.open(file, FileMode.WRITE);
			fs.writeUTFBytes(fileContent);
			fs.close();
			
			
		}
	}
}